#include "FlyingEnemy.h"
#include "../DeadlyShadowsLastHope/Game.h"
#include "../DeadlyShadowsLastHope/CreateBulletMessage.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../DeadlyShadowsLastHope/Bullet.h"

#define SHOT_DELAY 0.25f
#define MOVEMENT_DELAY 2.5f

CFlyingEnemy::CFlyingEnemy(void)
{
	m_eType = OBJ_FLYING_ENEMY;

	m_bFaceing			= 0;
	m_fTimer			= 0;
	m_nHealth			= 0;
	m_fShootTimer		= 0;
	m_nEnemyShotSfxID	= -1;
	m_fMovementTimer	= 0;
	m_fWaypoint01		= CGame::GetInstance()->GetWidth() * 0.9f;
	m_fWaypoint02		= CGame::GetInstance()->GetWidth() * 0.5f;
	m_fWaypoint03		= CGame::GetInstance()->GetWidth() * 0.1f;
	WP1					= true;
	WP2					= false;
	WP3					= false;

	
	m_cAniInfo.SetName("EFlyWalk");
	m_cAniInfo.SetNext("EFlyAttack");
	m_cAniInfo.SetTime(0);
	m_cAniInfo.SetFrame(0);

	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pMS	= CMessageSystem::GetInstance();
	m_pAM	= CAnimationManager::GetInstance();

	//m_nEnemyShotSfxID = m_pXA->SFXLoadSound(_T("resource/sounds/...load sound here...") );
}

CFlyingEnemy::~CFlyingEnemy(void)
{
	//if( m_nEnemyShotSfxID != -1 )
	//{
	//m_pXA->SFXStopSound( m_nEnemyShotSfxID );
	//m_pXA->SFXUnloadSound( m_nEnemyShotSfxID );
	//m_nEnemyShotSfxID = -1;
	//}
}

//Inherited from CEntity///////////////////////////////
void CFlyingEnemy::Update	(float fElapsedTime)
{
	m_fShootTimer += fElapsedTime * 0.5f;

//CREATE FIRING BULLETS && THE SHOT SOUND===============
	if (this->m_fShootTimer > SHOT_DELAY)
	{
		this->m_fShootTimer = 0.0f;
		CCreateBulletMessage* pMsg = new CCreateBulletMessage(this);
		m_pMS->SendMsg( pMsg );
		pMsg = nullptr;

		if(this->m_pXA->SFXIsSoundPlaying( m_nEnemyShotSfxID ) == false )
			this->m_pXA->SFXPlaySound( m_nEnemyShotSfxID );
	}
//CREATE WAYPOINTS AND MOVEMENT==========================
	if (this->m_fMovementTimer > MOVEMENT_DELAY)
	{
		if (FloatEquals(GetPosX(),m_fWaypoint01, 32) && WP1)
		{
			SetVelX(CGame::GetInstance()->GetWidth() * 0.1f);
			WP2 = true;
			WP1 = false;
		}
		if (FloatEquals(GetPosX(), m_fWaypoint02, 32) && WP2)
		{
			SetVelX(CGame::GetInstance()->GetWidth() * -0.1f);
			WP2 = false;
			WP3 = true;
		}
		if (FloatEquals(GetPosX(),m_fWaypoint03, 32) && WP3)
		{
			SetVelX(CGame::GetInstance()->GetWidth() * 0.1f);
			WP2 = false;
			WP1 = true;
		}
	}

	CEntity::Update(fElapsedTime);

		if (FloatEquals(GetPosX(), m_fWaypoint02, 32) && WP2 && GetVelX() != 0.0f)
		{
			m_fMovementTimer  = 0.0f;
			SetVelX(0.0f);
		}
		if (FloatEquals(GetPosX(), m_fWaypoint03, 32) && WP3 && GetVelX() != 0.0f)
		{
			m_fMovementTimer  = 0.0f;
			SetVelY(0.0f);
		}
		if (FloatEquals(GetPosX(), m_fWaypoint01, 32) && WP1 && GetVelX() != 0.0f)
		{
			m_fMovementTimer  = 0.0f;
			SetVelX(0.0f);
		}

m_pAM->Update(fElapsedTime,this->m_cAniInfo);

this->SetHeight(m_pAM->GetCHeight(m_cAniInfo));
this->SetWidth(m_pAM->GetCWidth(m_cAniInfo));

}

void CFlyingEnemy::Render	(void)
{
	//might not need this function. (depending on animations)
//	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();

//	RECT rSource = { 0, 0, 32, 32 };
//	pTM->Draw( GetImageID(), (int)GetPosX() -int(GetWidth()*0.5f), (int)GetPosY() -int(GetHeight()*0.5f), 1.1f, 1.0f, &rSource, (GetWidth()*0.5f), (GetHeight()*0.5f), 0);
	m_pAM->Render(m_cAniInfo,(int)GetPosX() -int(GetWidth()*0.5f),(int)GetPosY() -int(GetHeight()*0.5f),1.0f,this->GetFacing());
}

RECT CFlyingEnemy::GetRect(void) const
{
	RECT collision = {(int)this->GetPosX()- int(GetWidth()*0.5f), (int)this->GetPosY()-int(GetHeight()*0.5), (int)this->GetPosX()+ int(GetWidth()*0.5f), (int)this->GetPosY()+int(GetHeight()*0.5f)  };
	return collision;
}

bool CFlyingEnemy::CheckCollision( IEntity* pOther )
{
	CBullet* pBullet = dynamic_cast< CBullet* > (pOther);
	if (CEntity::CheckCollision(pOther))
	{
		if (pOther->GetType() == OBJ_BULLET)
		{
			/*
				CDecreaseHealth* pMsg = new CDecreaseHealth(this, -10);
				CSGD_MessageSystem::GetInstance()->SendMsg( pMsg );
				pMsg = nullptr;
			*/
		}
		return true;
	}
	return false;
}
///////////////////////////////////////////////////////

//Inherited from CEnemy///////////////////////////////
void CFlyingEnemy::HandleEvent(CNEvent* pEvent)
{

}
